﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using BlueLotus7.ArchiveSystem.Common.Contracts.Sina.Weibo;
using System.Text.RegularExpressions;

namespace BlueLotus7.ArchiveSystem.SinaContentApi.Weibo
{
    partial class WeiboContentRetriever
    {
        public List<WeiboArticle> SearchWeiboArticles(string keyword, string userNick, bool? isOriginal, bool? hasImage, DateTime? startTime, DateTime? endTime)
        {
            const string timeFormat = "yyyyMMdd";
            const string refer = UrlGenerator.host + "/search/mblog/?advanced=1&f=s&rl=1";
            string url = UrlGenerator.GenerateSearchArticleByCriterias();

            //Prepare search criterias
            Dictionary<string, string> postData = new Dictionary<string, string>();
            postData.Add("advancedfilter", "1");
            if (!string.IsNullOrWhiteSpace(keyword))
            {
                postData.Add("keyword", keyword);
            }
            if (!string.IsNullOrWhiteSpace(userNick))
            {
                postData.Add("nick", userNick);
            }
            postData.Add("hasori", isOriginal == null ? "0" : (isOriginal.Value ? "1" : "2"));
            postData.Add("haspic", hasImage == null ? "0" : (hasImage.Value ? "1" : "2"));

            if (startTime != null)
            {
                postData.Add("starttime", startTime.Value.ToString(timeFormat));
            }
            if (endTime != null)
            {
                postData.Add("endtime", endTime.Value.ToString(timeFormat));
            }
            postData.Add("smblog", "搜索");

            string pageContent = RetrieveContentByUrl(url, postData, refer);

            List<WeiboArticle> result = new List<WeiboArticle>();

            //Todo

            return result;
        }
    }
}
